public class Main {
    public static void main(String[] args) {
        int[] prices = new int[]{7,1,5,3,6,4};
        System.out.println(maxProfit(prices));
    }
    public static int maxProfit(int[] prices) {
        int max = 0;
        int i = 0;
        for (int j = i;j < prices.length; j++) {
            //如果i比i+1位置大，此时不能买入股票,i向后走
            if(j+1 != prices.length && prices[i] > prices[i+1]) {
                i++;
                continue;
            }
            //说明i到j是上升的,计算最大值累加到一起
            if(j+1 != prices.length && prices[j] > prices[j+1] ) {
                max = max + prices[j] - prices[i];
                i = j+1;
            }
            //当j走到最后一个位置时
            else if(j+1 == prices.length && prices[j] - prices[i] > 0){
                max = max+ prices[j] - prices[i];
                break;
            }
        }
        return max;
    }
}
